<?php
class systemApp extends core
{
	//生成信息页
    public function makeAction($arg)
    {
    	$this->wid		= $arg['wid'];
		$this->classid	= @$arg['classid'];
		$this->id		= @$arg['id'];
		$this->action	= explode(',', $arg['action']);
		$this->columns	= base::init('column')->fetchAll(array('wid'=>$this->wid));
		$this->mode		= sort_combine(base::init('mode')->fetchAll(), 'mid', true);
		$this->nums		= 0;
		
		$core			= new core;
		$this->template	= $core->template();
	    
		if (in_array('index',$this->action))
		{
			$this->nums++;
		}
		if (in_array('column',$this->action))
		{
			if ('wid' == $this->classid)
			{
				$this->classid	= array_get_unique($this->columns, 'classid');
			}
			else
			{
				$this->classid	= explode(',', $this->classid);
			}
			$this->nums	+= base::init('column')->getCount(array('classid'=>$this->classid));
		}
		if (in_array('page',$this->action))
		{
			if ('wid' == $this->id)
			{
				$this->classid	= array_get_unique($this->columns, 'classid');
				$getCountQuery	= array('classid'=>$this->classid);
			}
			else if ('column' == $this->id)
			{
				$getCountQuery	= array('classid'=>$this->classid);
			}
			else 
			{
				$this->id		= explode(',', $this->id);
				$getCountQuery	= array('id'=>$this->id);
			}
			
			$modid	= base::init('column')->fetchModids($this->wid);
        	$count	= 0 ;

        	foreach ($modid as $val)
        	{
        		$tbname	= $this->mode[$val]['tbname'];
        		$this->nums	+= base::init($tbname)->getCount($getCountQuery);
        	}
		}
		
		$this->template->assign('wid', ($this->wid));
	    $this->template->assign('classid', json_encode($this->classid));
	    $this->template->assign('id', json_encode($this->id));
	    $this->template->assign('action', json_encode($this->action));
    	$this->template->assign('nums', $this->nums);
    	$this->template->display('system_make.php');
    }
    
    public function toMakeAction($arg)
    {
    	ignore_user_abort(true);
    	$this->print	= isset($arg['print']) ? $arg['print'] : 1;
    	$this->wid		= $arg['wid'];
		$this->classid	= @$arg['classid'];
		$this->id		= @$arg['id'];
		$this->action	= explode(',', $arg['action']);
		$this->columns	= base::init('column')->fetchAll(array('wid'=>$this->wid));
		$this->mode		= sort_combine(base::init('mode')->fetchAll(), 'mid', true);
		$this->nums		= 0;
		$this->website	= base::init('website')->fetchRow(array('wid'=>$this->wid));
		$this->attr		= base::init('website')->getConfig($this->website['domain']);
    	$json			= array();
    	
    	if (in_array('index',$this->action))
		{
			$this->nums++;
			$json['title']	= array('首页');
			$json['url']	= array($this->attr['url']);
		}
		if (in_array('column',$this->action))
		{
			if ('wid' == $this->classid)
			{
				$this->classid	= array_get_unique($this->columns, 'classid');
			}
			else
			{
				$this->classid	= (array)$this->classid;
			}
			$this->nums	+= base::init('column')->getCount(array('classid'=>$this->classid));
			foreach ($this->classid as $classid)
			{
				$json['title'][]	= $this->columns[$classid]['classname'];
				$json['url'][]		= $this->attr['url'] . $this->columns[$classid]['classpath'];
			}
		}
		if (in_array('page',$this->action))
		{
			if ('wid' == $this->id)
			{
				$this->classid	= array_get_unique($this->columns, 'classid');
				$getCountQuery	= array('classid'=>$this->classid);
			}
			else if ('classid' == $this->id)
			{
				$getCountQuery	= array('classid'=>$this->classid);
			}
			else 
			{
				$this->id		= explode(',', $this->id);
				$getCountQuery	= array('id'=>$this->id);
			}
			
			$modid	= base::init('column')->fetchModids($this->wid);
        	$count	= 0 ;

        	foreach ($modid as $val)
        	{
        		$tbname	= $this->mode[$val]['tbname'];
        		$this->nums	+= base::init($tbname)->getCount($getCountQuery);
        		$list	= base::init($tbname)->fetchAll($getCountQuery);
        		$list	= base::init($tbname)->getTempList($list,$this->columns, $this->attr);
        		
        		foreach($list as $listVal)
        		{
        			$json['title'][]	= $listVal['title'];
					$json['url'][]		= $listVal['link'];
        		}
        		
        	}
		}
    	base::init('system')->make(
    		$this->action,
    		$this->wid,
    		$this->classid,
    		$this->id,
    		'make',
    		0
    	);

    	$json['made']	= $this->nums;
    	die(json_encode($json));
    }
    
    public function previewAction($arg)
    {
    	@base::init('system')->make(
    		explode(',', $arg['action']),
    		$arg['wid'],
    		explode(',', $arg['classid']),
    		$arg['id'],
    		'preview',
    		0
    	);
    }
    
    
	public function sitemapAction($arg)
	{
		$website	= base::init('website')->fetchRow(array('wid'=>$arg['wid']));
		$columns    = base::init('column')->fetchAll(array('wid'=>$arg['wid'],'display'=>1));
		$columns	= base::init('column')->listcls($columns,0);
		$attr		= base::init('website')->getConfig($website['domain']);

		$temp       = isset($attr['indextemp']) ? $attr['indextemp'] : '';
		$temp_file  = ROOT . $attr['tempdir'] . $temp;
		$path		= ROOT . $attr['arcdir'];
		$file_path	= $path . '/index' . $attr['indextype'];

		!empty($temp)			|| die('Template path is undefined');
		is_file($temp_file)		|| die($temp_file . ' is not found');
		is_dir($path)			|| die($path . ' does not exist');

		$template	= $this->template();
		
		$template->assign('columns', $columns);
		$template->assign('website', $attr);
		$template->assign('attr', $attr);
	    //print_r($attr);
		header('Content-Type: text/xml;charset=UTF-8');
		print_r(trim($template->fetch('sitemap.tpl')));
	}
}
